package com.zrrd.yunchmall.product.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zrrd.yunchmall.product.entity.Brand;
import com.zrrd.yunchmall.product.service.IBrandService;
import com.zrrd.yunchmall.util.ResponseResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;


/**
 * <p>
 * 品牌表 前端控制器
 * </p>
 *
 * @author LYY
 * @since 2024-01-15
 */
@RestController
@RequestMapping("/brand")
@Api(tags = "商品模块-品牌管理")
public class BrandController {
    @Autowired
    private IBrandService brandService;

    @ApiOperation("品牌列表")
    @GetMapping("/list")
    private ResponseResult list(@RequestParam(required = false) String keyword,
                                @RequestParam int pageNum,
                                @RequestParam int pageSize){
        QueryWrapper queryWrapper=new QueryWrapper<>();
        //后期把视频和品牌数据放到ES中存储，提高模糊查询的效率，降低Mysql压力
        if (!StringUtils.isEmpty(keyword)){
            queryWrapper.like("name",keyword);
        }
        return new ResponseResult<>(200,"查询成功！",brandService.page(new Page<>(pageNum,pageSize),queryWrapper));
    }
    @PostMapping("/update/sort/{id}")
    @ApiOperation("查询新品列表")
    private ResponseResult update(@RequestParam @PathVariable("id") int id,@RequestParam int sort){
        UpdateWrapper updateWrapper=new UpdateWrapper<>();
        updateWrapper.set("sort",sort);
        updateWrapper.in("id",id);
        brandService.update(updateWrapper);
        return new ResponseResult<>(200,"修改成功!");
    }
   @PostMapping("/update/factoryStatus")
    @ApiOperation("品牌制造商")
    private ResponseResult updateFactoryStatus(String ids,Integer factoryStatus){
        for (String id:ids.split(",")) {
            UpdateWrapper updateWrapper=new UpdateWrapper<>();
            updateWrapper.set("factory_status",factoryStatus);
            updateWrapper.in("id",Long.valueOf(id));
            brandService.update(updateWrapper);
        }
        return new ResponseResult<>(200,"修改成功!");
    }
   @PostMapping("/update/showStatus")
    @ApiOperation("是否显示")
    private ResponseResult updateShowStatus(String ids,Integer showStatus){
        for (String id:ids.split(",")) {
            UpdateWrapper updateWrapper=new UpdateWrapper<>();
            updateWrapper.set("show_status",showStatus);
            updateWrapper.in("id",Long.valueOf(id));
            brandService.update(updateWrapper);
        }
        return new ResponseResult<>(200,"修改成功!");
    }
    @ApiOperation("编辑品牌")
    @GetMapping("/{id}")
    private ResponseResult list(@PathVariable("id") Integer id){
        return new ResponseResult<>(200,"查询成功！",brandService.getById(id));
    }

    @ApiOperation("编辑修改品牌")
    @PostMapping("/update/{id}")
    private ResponseResult UpdateList(@PathVariable("id") Integer id, @RequestBody Brand brand){
        UpdateWrapper updateWrapper=new UpdateWrapper<>();
        updateWrapper.eq("id",id);

        return new ResponseResult<>(200,"查询成功！",brandService.update(brand,updateWrapper));
    }
    @ApiOperation("删除品牌")
    @GetMapping("/delete/{id}")
    private ResponseResult delete(@PathVariable("id") Integer id){
        return new ResponseResult<>(200,"查询成功！",brandService.removeById(id));
    }
    @ApiOperation("删除品牌")
    @PostMapping("/create")
    private ResponseResult create(@RequestBody Brand brand){
        return new ResponseResult<>(200,"查询成功！",brandService.save(brand));
    }
}
